package edu.utd.text.game.designer.commands;

import java.util.Scanner;

import edu.utd.text.game.designer.controller.DesignerController;
import edu.utd.text.game.domain.GameObject;
import edu.utd.text.game.domain.areas.Room;

public class DisplayRoomCommand implements DesignerCommand {
	private String roomName;
	
	public DisplayRoomCommand() {  }
	
	public DisplayRoomCommand(String roomName) {
		this.roomName = roomName;
	} 
	
	public String execute() throws Exception {
		Scanner scanner = new Scanner(System.in);
		if(null == this.roomName) {
			System.out.println("Name of room to display: ");
			this.roomName = scanner.nextLine().trim();
		}
		Room roomToDisplay = 
			DesignerController.INSTANCE.getGameMap().getRoomByName(this.roomName);
		
		if (null == roomToDisplay) {
			return "No room by the name \"" + this.roomName
				+ "\" exists in the game map.";
		}
		
		String displayString = "======================= \nROOM: \"" 
			+ roomToDisplay.getRoomName() + "\"\n=======================\n"
			+ "DESCRIPTION:\n\"" 
			+ roomToDisplay.getDescription() + "\"\n======================="
			+ "\nROOM CONTAINS OBJECTS:\n" + 
			
			(roomToDisplay.getObjects().isEmpty() 
					? 
						"No objects." 
					: 
						formatRoomContents(roomToDisplay)
			)
			
			+ "\n======================="
			;
		
		return displayString;
	}
	
	private String formatRoomContents(Room room) {
		String roomContents = "";
		for (GameObject object : room.getObjects()) {
			roomContents += object.getName() + "\n";
		}
		return roomContents.trim();
	}

}
